import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:zrjz/comm/constants/constants.dart';
import 'package:zrjz/comm/widget/my_text_view.dart';

import '../../constants/color_constants.dart';

class DataPickerUtil {
  static void showCupertinoDataPicker({
    required List<String> items,
    required Function(List<String>, int) onPickerChanged,
    int? initialSelectIndex = 0,
    String? title,
    String? confirmTextStr,
    String? cancelTextStr,
    Function()? onReset,
  }) {
    SmartDialog.show(
      usePenetrate: false,
      tag: 'data_picker',
      maskColor: ColorConstants.black.withOpacity(0.4),
      alignment: Alignment.bottomCenter,
      builder: (BuildContext context) {
        return Container(
          height: 280,
          decoration: BoxDecoration(
            color: DarkThemeUtil.multiColors(ColorConstants.white, darkColor: ColorConstants.darkBlackItem),
            borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
          ),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CupertinoButton(
                    child: Text(
                      cancelTextStr ?? '取消'.tr,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: ColorConstants.appBlue,
                      ),
                    ),
                    onPressed: onReset ??
                        () {
                          SmartDialog.dismiss(tag: 'data_picker');
                        },
                  ),
                  Text(
                    title ?? "",
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                      color: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white),
                    ),
                  ),
                  CupertinoButton(
                    child: Text(
                      confirmTextStr ?? '确定'.tr,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: ColorConstants.appBlue,
                      ),
                    ),
                    onPressed: () {
                      SmartDialog.dismiss(tag: 'data_picker');
                      // 将选中的值通过回调函数传递出去
                      onPickerChanged(items, initialSelectIndex!);
                    },
                  ),
                ],
              ),
              const Divider(color: ColorConstants.dividerD9, height: 0.5),
              // SizedBox(
              //   height: 200,
              //   child: CupertinoPicker(
              //     itemExtent: 45,
              //     diameterRatio: 1,
              //     onSelectedItemChanged: (int value) {
              //       initialSelectIndex = value;
              //     },
              //     children: items
              //         .map((item) => Container(
              //               alignment: Alignment.center,
              //               child: MyTextView(
              //                 item.toString(),
              //                 textAlign: TextAlign.center,
              //                 textColor: ColorConstants.black,
              //                 fontSize: 16,
              //                 isFontBold: true,
              //               ),
              //             ))
              //         .toList(),
              //   ),
              // ),
              Expanded(
                  child: Container(
                alignment: Alignment.center,
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(initialItem: initialSelectIndex ?? 0),
                  itemExtent: 45,
                  diameterRatio: 1,
                  selectionOverlay: const CupertinoPickerDefaultSelectionOverlay(
                    capStartEdge: false,
                    capEndEdge: false,
                  ),
                  onSelectedItemChanged: (int value) {
                    initialSelectIndex = value;
                  },
                  children: items
                      .map((item) => Container(
                            alignment: Alignment.center,
                            child: MyTextView(
                              item.toString(),
                              textAlign: TextAlign.center,
                              textColor: DarkThemeUtil.multiColors(
                                ColorConstants.black404A5B,
                                darkColor: ColorConstants.white,
                              ),
                              fontSize: 16,
                              isFontBold: true,
                            ),
                          ))
                      .toList(),
                ),
              )),
            ],
          ),
        );
      },
    );
  }
}
